package com.ruikc.study.demo.service;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class CacheService {

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    // 设置缓存（自动设置过期时间：1小时）
    public void setCache(String key, Object data) {
        redisTemplate.opsForValue().set(key, data, 3600, TimeUnit.SECONDS);
    }

    public void setCache(String key, Object data, int seconds) {
        redisTemplate.opsForValue().set(key, data, seconds, TimeUnit.SECONDS);
    }

    // 获取缓存
    public Object getCache(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    // 删除缓存
    public void deleteCache(String key) {
        redisTemplate.delete(key);
    }

    // 批量删除（可选）
    public void deleteBatch(List<String> keys) {
        redisTemplate.delete(keys);
    }
}
